// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/type_with_id.h>
#include <pollux/dwio/dwrf/common/encryption_common.h>

namespace kumo::pollux::dwrf::encryption {

class EncryptionHandler
    : public EncryptionHandlerBase<dwio::common::encryption::Encrypter> {
 public:
  static std::unique_ptr<EncryptionHandler> create(
      const std::shared_ptr<const Type>& schema,
      const EncryptionSpecification& spec,
      dwio::common::encryption::EncrypterFactory* factory = nullptr) {
    return create(dwio::common::TypeWithId::create(schema), spec, factory);
  }

  static std::unique_ptr<EncryptionHandler> create(
      const std::shared_ptr<const dwio::common::TypeWithId>& schema,
      const EncryptionSpecification& spec,
      dwio::common::encryption::EncrypterFactory* factory = nullptr);

 private:
  void populateNodeMaps(
      const dwio::common::TypeWithId& root,
      uint32_t groupIndex);

  void populateChildNodeMap(
      uint32_t root,
      const dwio::common::TypeWithId& node);

  uint32_t addEncrypter(
      dwio::common::encryption::EncrypterFactory& factory,
      const dwio::common::encryption::EncryptionProperties& props);
};

} // namespace kumo::pollux::dwrf::encryption
